//
//  WJPersonalCenterViewController.m
//  BeDeJie
//
//  Created by jerry on 2017/2/22.
//  Copyright © 2017年 jerry. All rights reserved.
//

#import "WJPersonalCenterViewController.h"

@interface WJPersonalCenterViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *headImageView;

@end

@implementation WJPersonalCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人中心";
    
}

- (IBAction)setHeadImage:(id)sender {
    // UIAlertControllerStyleAlert : 相当于弹出一个UIAlertView
    // UIAlertControllerStyleActionSheet : 相当于弹出一个UIActionSheet
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    // 相册按钮
    [alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openAlbum];
    }]];
    // 照相按钮
    [alert addAction:[UIAlertAction actionWithTitle:@"照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openCamera];
    }]];
    
    [self presentViewController:alert animated:YES completion:nil];
}

//打开相册
- (void)openAlbum {
    // UIImagePickerControllerSourceTypeSavedPhotosAlbum : 从Moments相册中选一张图片
    // UIImagePickerControllerSourceTypePhotoLibrary : 从所有相册中选一张图片
    // UIImagePickerControllerSourceTypeCamera : 利用照相机中拍一张图片
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

//打开照相机
- (void)openCamera {
    //若不是使用真机调用此方法，则返回
    //    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - <UIImagePickerControllerDelegate>
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    // 关闭图片选择界面
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    // 显示选择的图片
    self.headImageView.image = info[UIImagePickerControllerOriginalImage];
}


@end
